using System.Collections.ObjectModel;
using HMCollada.Core;
using System.Xml;

namespace HMCollada.Core {
    struct HMColladaPolylist {
        internal int count;
        internal string material;
        internal string name;
        internal Collection<HMColladaInput> input;
        internal int[] vcount;
        internal int[] p;
    }
}

namespace HMCollada {
    internal partial class HMColladaLoader {
        private static HMColladaPolylist LoadPolylist() {
            HMColladaPolylist newPolylist = new HMColladaPolylist();
            newPolylist.count = int.Parse(colladaReader.GetAttribute("count"));
            newPolylist.material = colladaReader.GetAttribute("material");
            newPolylist.name = colladaReader.GetAttribute("name");

            newPolylist.input = new Collection<HMColladaInput>();

            string content;
            string[] values;
            if (!colladaReader.IsEmptyElement) colladaReader.Read();
            do {
                if (colladaReader.NodeType != XmlNodeType.EndElement) {
                    switch (colladaReader.Name) {
                        case "input": newPolylist.input.Add(LoadInput()); break;
                        case "p":
                            content = ReadString().Trim();
                            while (content.Contains("  ")) content = content.Replace("  ", " ");
                            values = content.Split(new char[] { ' ' });
                            newPolylist.p = new int[values.Length];
                            for (int i = 0; i < newPolylist.p.Length; i++) newPolylist.p[i] = int.Parse(values[i]);
                            break;

                        case "vcount":
                            content = ReadString().Trim();
                            while (content.Contains("  ")) content = content.Replace("  ", " ");
                            values = content.Split(new char[] { ' ' });
                            newPolylist.vcount = new int[values.Length];
                            for (int i = 0; i < newPolylist.vcount.Length; i++) newPolylist.vcount[i] = int.Parse(values[i]);
                            break;
                    }
                }

                if (!colladaReader.IsEmptyElement) colladaReader.Read();
            } while (colladaReader.Name != "polylist");

            if (colladaReader.IsEmptyElement) colladaReader.Read();
            return newPolylist;
        }
    }
}